using System.Numerics;
using System.Text.Json;

namespace OracleInternal.Json.Common
{
	internal class JsonpGeneratorWrapper
	{
		private IOracleJsonGenerator m_wrapped;

		public JsonpGeneratorWrapper(IOracleJsonGenerator wrapped)
		{
			m_wrapped = wrapped;
		}

		public void Close()
		{
			try
			{
				m_wrapped.Close();
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public void Flush()
		{
			try
			{
				m_wrapped.Flush();
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteKey(string key)
		{
			try
			{
				m_wrapped.WriteKey(key);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		private JsonException Translate(OracleJsonException e)
		{
			return new JsonException(e.Message, e);
		}

		public void WriteJsonParser(Utf8JsonReader parser)
		{
			int num = 0;
			while (parser.Read())
			{
				switch (parser.TokenType)
				{
				case JsonTokenType.EndArray:
					WriteEnd();
					num--;
					break;
				case JsonTokenType.EndObject:
					WriteEnd();
					num--;
					break;
				case JsonTokenType.PropertyName:
					WriteKey(parser.GetString());
					break;
				case JsonTokenType.StartArray:
					WriteStartArray();
					num++;
					break;
				case JsonTokenType.StartObject:
					WriteStartObject();
					num++;
					break;
				case JsonTokenType.False:
					Write(arg: false);
					break;
				case JsonTokenType.Null:
					WriteNull();
					break;
				case JsonTokenType.Number:
					Write(parser.GetDecimal());
					break;
				case JsonTokenType.String:
					Write(parser.GetString());
					break;
				case JsonTokenType.True:
					Write(arg: true);
					break;
				}
				if (num <= 0)
				{
					break;
				}
			}
		}

		public JsonpGeneratorWrapper Write(string arg)
		{
			try
			{
				m_wrapped.Write(arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(decimal arg)
		{
			try
			{
				m_wrapped.Write(arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(BigInteger arg)
		{
			m_wrapped.Write(arg);
			return this;
		}

		public JsonpGeneratorWrapper Write(int arg)
		{
			try
			{
				m_wrapped.Write(arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(long arg)
		{
			try
			{
				m_wrapped.Write(arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(double arg)
		{
			try
			{
				m_wrapped.Write(arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(bool arg)
		{
			try
			{
				m_wrapped.Write(arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, string arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, BigInteger arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, decimal arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, int arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, long arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, double arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper Write(string key, bool arg)
		{
			try
			{
				m_wrapped.Write(key, arg);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteEnd()
		{
			try
			{
				m_wrapped.WriteEnd();
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteNull()
		{
			try
			{
				m_wrapped.WriteNull();
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteNull(string key)
		{
			try
			{
				m_wrapped.WriteNull(key);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteStartArray()
		{
			try
			{
				m_wrapped.WriteStartArray();
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteStartArray(string key)
		{
			try
			{
				m_wrapped.WriteStartArray(key);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteStartObject()
		{
			try
			{
				m_wrapped.WriteStartObject();
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}

		public JsonpGeneratorWrapper WriteStartObject(string key)
		{
			try
			{
				m_wrapped.WriteStartObject(key);
				return this;
			}
			catch (OracleJsonException e)
			{
				throw Translate(e);
			}
		}
	}
}
